#!/usr/bin/env python3
import re

# 定义旧路径到新路径的映射
path_mapping = {
    "res://scripts/language_manager.gd": "res://scripts/core/language_manager.gd",
    "res://scripts/dialog_manager.gd": "res://scripts/dialog/dialog_manager.gd",
    "res://scripts/inventory_manager.gd": "res://scripts/inventory/inventory_manager.gd",
    "res://scripts/item_database.gd": "res://scripts/inventory/item_database.gd"
}

# 读取project.godot文件
try:
    with open("project.godot", "r", encoding="utf-8") as file:
        content = file.read()
except Exception as e:
    print(f"读取project.godot文件时出错: {e}")
    exit(1)

# 检查并替换autoload引用
updated_content = content
for old_path, new_path in path_mapping.items():
    # 使用正则表达式匹配autoload条目
    pattern = f'([A-Za-z0-9]+)="\\*{old_path}"'
    match = re.search(pattern, content)
    
    if match:
        autoload_name = match.group(1)
        old_entry = f'{autoload_name}="*{old_path}"'
        new_entry = f'{autoload_name}="*{new_path}"'
        
        updated_content = updated_content.replace(old_entry, new_entry)
        print(f"更新autoload引用: {old_entry} -> {new_entry}")

# 如果有更改，写回文件
if updated_content != content:
    try:
        with open("project.godot", "w", encoding="utf-8") as file:
            file.write(updated_content)
        print("成功更新project.godot文件")
    except Exception as e:
        print(f"写入project.godot文件时出错: {e}")
        exit(1)
else:
    print("没有找到需要更新的引用") 